package com.codeavatar.catools.tool;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.codeavatar.catools.base.CaBaseApplication;
import com.codeavatar.catools.helper.CaLogHelper;

/**
 * +----------------------------------------------------------------------
 * | @Author: codeavatar   @Year：2022
 * +----------------------------------------------------------------------
 * | @Email: codeavatar@aliyun.com
 * +----------------------------------------------------------------------
 **/
public class CaAppTool {

    private final String TAG = this.getClass().getSimpleName();

    //++++++++++++++++++++++++++++++++++++++
    //++ 外部方法
    //++++++++++++++++++++++++++++++++++++++

    public String getPackageName(){
        PackageInfo pi = this.getPackageInfo();
        if(null == pi) return "";
        return pi.packageName;
    }

    public String getVersionName(){
        PackageInfo pi = this.getPackageInfo();
        if(null == pi) return "";
        return pi.versionName;
    }

    public int getVersionCode(){
        PackageInfo pi = this.getPackageInfo();
        if(null == pi) return 0;
        return pi.versionCode;
    }

    public PackageInfo getPackageInfo(){
        PackageManager pm = CaBaseApplication.getContext().getPackageManager();
        try {
            return pm.getPackageInfo(CaBaseApplication.getContext().getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            CaLogHelper.error(TAG,e);
        } catch (Exception e){
            CaLogHelper.error(TAG,e);
        }
        return null;
    }

    private static CaAppTool init;
    public static CaAppTool init(){
        if(null == init){
            init = new CaAppTool();
        }
        return init;
    }

    //++++++++++++++++++++++++++++++++++++++
    //++ 内部方法
    //++++++++++++++++++++++++++++++++++++++

    private CaAppTool(){
        throw new UnsupportedOperationException("Initialization is not supported");
    }
}
